| Conditions | 1 |
| Paths | 1 |
| Total Lines | 141 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | module.exports = function(GedcomX){ |
||
| 2 | |||
| 3 | var utils = require('../utils'), |
||
| 4 | Base = require('../Base'); |
||
| 5 | |||
| 6 | /** |
||
| 7 | * A list of Links |
||
| 8 | * |
||
| 9 | * @constructor |
||
| 10 | * @param {Object} [json] |
||
|
|
|||
| 11 | */ |
||
| 12 | var Links = function(json){ |
||
| 13 | |||
| 14 | // Protect against forgetting the new keyword when calling the constructor |
||
| 15 | if(!(this instanceof Links)){ |
||
| 16 | return new Links(json); |
||
| 17 | } |
||
| 18 | |||
| 19 | // If the given object is already an instance then just return it. DON'T copy it. |
||
| 20 | if(Links.isInstance(json)){ |
||
| 21 | return json; |
||
| 22 | } |
||
| 23 | |||
| 24 | this.init(json); |
||
| 25 | }; |
||
| 26 | |||
| 27 | // There's no value in us extending Base at the moment |
||
| 28 | Links.prototype = Object.create(Base.prototype); |
||
| 29 | |||
| 30 | Links._gedxClass = Links.prototype._gedxClass = 'GedcomX.Links'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Check whether the given object is an instance of this class. |
||
| 34 | * |
||
| 35 | * @param {Object} obj |
||
| 36 | * @returns {Boolean} |
||
| 37 | */ |
||
| 38 | Links.isInstance = function(obj){ |
||
| 39 | return utils.isInstance(obj, this._gedxClass); |
||
| 40 | }; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Initialize from JSON |
||
| 44 | * |
||
| 45 | * @param {Object} |
||
| 46 | * @return {Link} this |
||
| 47 | */ |
||
| 48 | Links.prototype.init = function(json){ |
||
| 49 | |||
| 50 | Base.prototype.init.call(this, json); |
||
| 51 | |||
| 52 | if(json){ |
||
| 53 | this.setLinks(json); |
||
| 54 | } |
||
| 55 | return this; |
||
| 56 | }; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Get the list of links |
||
| 60 | * |
||
| 61 | * @return {Link[]} links |
||
| 62 | */ |
||
| 63 | Links.prototype.getLinks = function(){ |
||
| 64 | return this.links; |
||
| 65 | }; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Get a link matching a rel |
||
| 69 | * |
||
| 70 | * @param {String} rel |
||
| 71 | * @return {Link} link |
||
| 72 | */ |
||
| 73 | Links.prototype.getLink = function(rel){ |
||
| 74 | return this.links.find(function(link){ |
||
| 75 | return link.getRel() === rel; |
||
| 76 | }); |
||
| 77 | }; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Add a link |
||
| 81 | * |
||
| 82 | * @param {Link} link |
||
| 83 | * @return {Links} this |
||
| 84 | */ |
||
| 85 | Links.prototype.addLink = function(link){ |
||
| 86 | // TODO: check for duplicates |
||
| 87 | this.links.push(GedcomX.Link(link)); |
||
| 88 | return this; |
||
| 89 | }; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Set the links. May either provide the JSON object structure or an array |
||
| 93 | * of Link instances |
||
| 94 | * |
||
| 95 | * @param {Object|Link[]} links |
||
| 96 | * @return {Links} this |
||
| 97 | */ |
||
| 98 | Links.prototype.setLinks = function(links){ |
||
| 99 | this.links = []; |
||
| 100 | if(links){ |
||
| 101 | |||
| 102 | // List of link |
||
| 103 | if(Array.isArray(links)){ |
||
| 104 | for(var i = 0; i < links.length; i++){ |
||
| 105 | this.addLink(links[i]); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | // JSON object |
||
| 110 | else { |
||
| 111 | for(var rel in links){ |
||
| 112 | this.addLink(new GedcomX.Link(links[rel]).setRel(rel)); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | } |
||
| 116 | return this; |
||
| 117 | }; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Export the object as JSON |
||
| 121 | * |
||
| 122 | * @return {Object} JSON object |
||
| 123 | */ |
||
| 124 | Links.prototype.toJSON = function(){ |
||
| 125 | var links = this.getLinks(), |
||
| 126 | json = {}, |
||
| 127 | linkJson, rel; |
||
| 128 | for(var i = 0; i < links.length; i++){ |
||
| 129 | linkJson = utils.toJSON(links[i]); |
||
| 130 | rel = linkJson.rel; |
||
| 131 | if(rel){ |
||
| 132 | delete linkJson.rel; |
||
| 133 | json[rel] = linkJson; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | return json; |
||
| 137 | }; |
||
| 138 | |||
| 139 | GedcomX.Links = Links; |
||
| 140 | |||
| 141 | }; |